﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Weibo.Web.Models;
using Weibo.Business;
using Weibo.Entity;
using Weibo.Config;
using Weibo.Common;

namespace Weibo.Web.Controllers
{
    public class SchoolController : BaseController
    {
        //
        // GET: /School/
        public ActionResult Index(Models.SchoolIndexModel model)
        {
            ViewData["CurHeader"] = "MySchool";
            if (model.Page == 0)
            {
                model.Page = 1;
            }
            model.PageSize = 30;
            if (!string.IsNullOrEmpty(model.site))
            {
                model.OtherUser = Users.GetUerByCustomSite(model.site);
            }
            if (!string.IsNullOrEmpty(model.nickname))
            {
                model.OtherUser = Users.GetUserByNickName(model.nickname);
            }
            if (model.uid > 0)
            {
                model.OtherUser = Users.GetUserByID(model.uid);
                if (model.OtherUser != null && !Request.IsAjaxRequest())
                {
                    if (!string.IsNullOrEmpty(model.OtherUser.CustomSite))
                    {
                        return Redirect("/" + model.OtherUser.CustomSite);
                    }
                }
            }
            if (model.OtherUser != null)
            {
                if (CurrentUser != null)
                {
                    if (CurrentUser.ID == model.OtherUser.ID)
                    {
                        model.CurUser = CurrentUser;
                        if (!Request.IsAjaxRequest())
                        {

                            model.CurSkinCss = CurrentCssUrl;
                            model.CurUserConfig = CurrentUserConfig;
                        }
                        else
                        {
                            model.CurrentSchoolClassID = model.gid;
                            if (model.gid == 0 && model.IsFriendShip == 0)
                            {
                                model.IsMyFollow = 1;
                                model.RecordCount = 0;
                                model.EducationList = Educations.GetEducationsByUID(model.CurUser.ID);
                                model.NotAuditEducationList = Educations.GetEducationsByIsVisible(model.CurUser.ID, 0);
                                string dataSchoolList = RenderRazorViewToString("~/Views/School/MyAjaxSchoolList.cshtml", model);
                                if (!string.IsNullOrEmpty(dataSchoolList))
                                {
                                    return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataSchoolList, model.RecordCount), JsonRequestBehavior.AllowGet);
                                }
                                else
                                {
                                    return Json(new JsonModel(CodeStruct.NoResult, dataSchoolList), JsonRequestBehavior.AllowGet);
                                }
                            }
                            int rowCount = 0;
                            if (model.IsHaveMember > 0)
                            {
                            
                                string firstNameChar = PageRequest.GetQueryString("firstNameChar");
                                //model.SchoolClassMember = Users.GetUsersByQuery(Bases.ToInt(model.gid), firstNameChar);
                                model.SchoolClassMember = Users.GetUserByPager(Bases.ToInt(model.gid), firstNameChar, model.Page,5, " order by ID desc ", ref rowCount);
                                model.RecordCount = rowCount;
                                string dataMember = RenderRazorViewToString("~/Views/School/ClassMemberList.cshtml", model);

                                if (model.SchoolClassMember!=null)
                                {
                                    model.RecordCount = (rowCount > 0 ? rowCount : 0);
                                    return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataMember, model.RecordCount), JsonRequestBehavior.AllowGet);
                                }
                                else
                                {
                                    model.RecordCount = 0;
                                    return Json(new JsonModel(CodeStruct.NoResult, dataMember), JsonRequestBehavior.AllowGet);
                                }
                            }
                            //if (model.IsHavePic > 0)
                            //{
                            //    model.RecordCount = rowCount;
                            //    string firstNameChar = PageRequest.GetQueryString("firstNameChar");
                            //    model.SchoolClassMember = Users.GetUsersByQuery(Bases.ToInt(model.gid), firstNameChar);
                            //    string dataMember = RenderRazorViewToString("~/Views/School/C1lassPhoto.cshtml", model);
                            //    if (!string.IsNullOrEmpty(dataMember))
                            //    {
                            //        return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataMember, model.RecordCount), JsonRequestBehavior.AllowGet);
                            //    }
                            //    else
                            //    {
                            //        return Json(new JsonModel(CodeStruct.NoResult, dataMember), JsonRequestBehavior.AllowGet);
                            //    }
                            //}
                            if (model.IsAdvanced > 0)
                            {
                                if (model.EndTime == null)
                                {
                                    model.EndTime = DateTime.MaxValue;
                                }
                                if (model.StartTime == null)
                                {
                                    model.StartTime = new DateTime(2000, 1, 1);
                                }
                                model.MiniBlogs = Schools.SearchSchoolMiniBlogForMbIndexAdvanced((model.Page - 1) * 3 + model.Buffer + 1, model.PageSize / 3, ref rowCount, model.Key, model.IsOri, model.IsRet, model.StartTime, model.EndTime, model.IsMyself, model.IsMyFollow, model.CurUser.ID, model.IsHavePic, model.IsHaveLink, model.IsHaveVideo, model.IsHaveMusic, model.IsHaveVote, model.Sort, model.gid, model.IsFriendShip);
                            }
                            else
                            {
                                model.MiniBlogs = Schools.SearchSchoolMiniBlog((model.Page - 1) * 3 + model.Buffer + 1, model.PageSize / 3, ref rowCount, model.Key, model.IsOri, model.Location, model.StartTime, model.EndTime, model.IsMyself, model.IsMyFollow, model.CurUser.ID, model.SomeOne, model.IsHavePic, model.IsHaveLink, model.IsHaveVideo, model.IsHaveMusic, model.IsHaveVote, model.Sort, model.gid, model.IsFriendShip);
                            }

                            model.RecordCount = rowCount;
                            string data = RenderRazorViewToString("~/Views/School/MyAjaxSchool.cshtml", model);
                            if (!string.IsNullOrEmpty(data))
                            {
                                return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, data, model.RecordCount), JsonRequestBehavior.AllowGet);
                            }
                            else
                            {
                                return Json(new JsonModel(CodeStruct.NoResult, data), JsonRequestBehavior.AllowGet);
                            }
                        }
                    }

                }

            }
            model.EducationList = Educations.GetEducationsByIsVisible(model.CurUser.ID, 1);

            return View("~/Views/School/Index.cshtml", model);
        }
        public ActionResult Class(Models.SchoolIndexModel model)
        {
            ViewData["CurHeader"] = "MySchool";
            if (model.Page == 0)
            {
                model.Page = 1;
            }
            model.PageSize = 30;
            if (!string.IsNullOrEmpty(model.site))
            {
                model.OtherUser = Users.GetUerByCustomSite(model.site);
            }
            if (!string.IsNullOrEmpty(model.nickname))
            {
                model.OtherUser = Users.GetUserByNickName(model.nickname);
            }
            if (model.uid > 0)
            {
                model.OtherUser = Users.GetUserByID(model.uid);
                if (model.OtherUser != null && !Request.IsAjaxRequest())
                {
                    if (!string.IsNullOrEmpty(model.OtherUser.CustomSite))
                    {
                        return Redirect("/" + model.OtherUser.CustomSite);
                    }
                }
            }
            if (model.OtherUser != null)
            {
                if (CurrentUser != null)
                {
                    if (CurrentUser.ID == model.OtherUser.ID)
                    {
                        model.CurUser = CurrentUser;
                        model.CurrentSchoolClassID = model.gid;
                        if (!Request.IsAjaxRequest())
                        {

                            model.CurSkinCss = CurrentCssUrl;
                            model.CurUserConfig = CurrentUserConfig;
                        }
                        else
                        {

                            if (model.gid == 0 && model.IsFriendShip == 0)
                            {
                                model.IsMyFollow = 1;
                                model.RecordCount = 0;
                                model.EducationList = Educations.GetEducationsByUID(model.CurUser.ID);
                                model.NotAuditEducationList = Educations.GetEducationsByIsVisible(model.CurUser.ID, 0);
                                string dataSchoolList = RenderRazorViewToString("~/Views/School/MyAjaxSchoolList.cshtml", model);
                                if (!string.IsNullOrEmpty(dataSchoolList))
                                {
                                    return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataSchoolList, model.RecordCount), JsonRequestBehavior.AllowGet);
                                }
                                else
                                {
                                    return Json(new JsonModel(CodeStruct.NoResult, dataSchoolList), JsonRequestBehavior.AllowGet);
                                }
                            }
                            int rowCount = 0;
                            if (model.IsHaveMember > 0)
                            {
                                model.RecordCount = rowCount;
                                string firstNameChar = PageRequest.GetQueryString("firstNameChar");
                                model.SchoolClassMember = Users.GetUsersByQuery(Bases.ToInt(model.gid), firstNameChar);
                                string dataMember = RenderRazorViewToString("~/Views/School/ClassMemberList.cshtml", model);
                                if (!string.IsNullOrEmpty(dataMember))
                                {
                                    return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataMember, model.RecordCount), JsonRequestBehavior.AllowGet);
                                }
                                else
                                {
                                    return Json(new JsonModel(CodeStruct.NoResult, dataMember), JsonRequestBehavior.AllowGet);
                                }
                            }
                            //if (model.IsHavePic > 0)
                            //{
                            //    model.RecordCount = rowCount;
                            //    string firstNameChar = PageRequest.GetQueryString("firstNameChar");
                            //    model.SchoolClassMember = Users.GetUsersByQuery(Bases.ToInt(model.gid), firstNameChar);
                            //    string dataMember = RenderRazorViewToString("~/Views/School/C1lassPhoto.cshtml", model);
                            //    if (!string.IsNullOrEmpty(dataMember))
                            //    {
                            //        return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, dataMember, model.RecordCount), JsonRequestBehavior.AllowGet);
                            //    }
                            //    else
                            //    {
                            //        return Json(new JsonModel(CodeStruct.NoResult, dataMember), JsonRequestBehavior.AllowGet);
                            //    }
                            //}
                            if (model.IsAdvanced > 0)
                            {
                                if (model.EndTime == null)
                                {
                                    model.EndTime = DateTime.MaxValue;
                                }
                                if (model.StartTime == null)
                                {
                                    model.StartTime = new DateTime(2000, 1, 1);
                                }
                                model.MiniBlogs = Schools.SearchSchoolMiniBlogForMbIndexAdvanced((model.Page - 1) * 3 + model.Buffer + 1, model.PageSize / 3, ref rowCount, model.Key, model.IsOri, model.IsRet, model.StartTime, model.EndTime, model.IsMyself, model.IsMyFollow, model.CurUser.ID, model.IsHavePic, model.IsHaveLink, model.IsHaveVideo, model.IsHaveMusic, model.IsHaveVote, model.Sort, model.gid, model.IsFriendShip);
                            }
                            else
                            {
                                model.MiniBlogs = Schools.SearchSchoolMiniBlog((model.Page - 1) * 3 + model.Buffer + 1, model.PageSize / 3, ref rowCount, model.Key, model.IsOri, model.Location, model.StartTime, model.EndTime, model.IsMyself, model.IsMyFollow, model.CurUser.ID, model.SomeOne, model.IsHavePic, model.IsHaveLink, model.IsHaveVideo, model.IsHaveMusic, model.IsHaveVote, model.Sort, model.gid, model.IsFriendShip);
                            }

                            model.RecordCount = rowCount;
                            string data = RenderRazorViewToString("~/Views/School/MyAjaxSchool.cshtml", model);
                            if (!string.IsNullOrEmpty(data))
                            {
                                return Json(new PagerJsonModel(CodeStruct.ReturnSuccess, data, model.RecordCount), JsonRequestBehavior.AllowGet);
                            }
                            else
                            {
                                return Json(new JsonModel(CodeStruct.NoResult, data), JsonRequestBehavior.AllowGet);
                            }
                        }
                    }

                }

            }
            model.EducationList = Educations.GetEducationsByIsVisible(model.CurUser.ID, 1);

            return View("~/Views/School/Class.cshtml", model);
        }
        public ActionResult Index1()
        {
            ;
            return View();
        }
        public ActionResult ClassPhoto(Models.ClassPhotoModel model)
        {
            ViewData["CurHeader"] = "MySchool";
            ViewData["URL"] = Request.Url.PathAndQuery;
            if (model.Page == 0)
            {
                model.Page = 1;
            }
            model.PageSize = 30;
            if (!string.IsNullOrEmpty(model.site))
            {
                model.OtherUser = Users.GetUerByCustomSite(model.site);
            }
            if (!string.IsNullOrEmpty(model.nickname))
            {
                model.OtherUser = Users.GetUserByNickName(model.nickname);
            }

            model.uid = CurrentUser.ID;

            model.OtherUser = Users.GetUserByID(model.uid);
            if (model.OtherUser != null && !Request.IsAjaxRequest())
            {
                if (!string.IsNullOrEmpty(model.OtherUser.CustomSite))
                {
                    return Redirect("/" + model.OtherUser.CustomSite);
                }
            }

            if (model.OtherUser != null)
            {
                if (CurrentUser != null)
                {
                    if (CurrentUser.ID == model.OtherUser.ID)
                    {
                        model.CurUser = CurrentUser;
                        if (!Request.IsAjaxRequest())
                        {

                            model.CurSkinCss = CurrentCssUrl;
                            model.CurUserConfig = CurrentUserConfig;
                        }
                    }
                }

            }
            if (model.classPhotoID > 0)
            {
                int rowCount = 0;
                model.PageSize = 24;
                model.ClassPhotoDetailsList = Schools.GetClassPhotoDetailList(model.classPhotoID, model.Page, model.PageSize, ref rowCount);
                model.RecordCount = rowCount;
                if (model.ClassPhotoDetailsList != null && model.ClassPhotoDetailsList.Count == 1 && model.ClassPhotoDetailsList[0].classPhotoID<1)
                {
                    model.RecordCount = 0;
                }
            }
        
            model.EducationList = Educations.GetEducationsByIsVisible(model.CurUser.ID, 1);
            model.ClassPhotoList = Schools.GetClassPhotoList(model.CurUser.ID, model.gid);
         
            return View("~/Views/School/ClassPhoto.cshtml", model);
        }
        public ActionResult FileCom()
        {
            ;
            return View();
        }
        public ActionResult Upload()
        {  ;
            try
            {
                HttpPostedFileBase postedFile = Request.Files["Filedata"];

                string savepath = "";
                string tempPath = "";
                tempPath = System.Configuration.ConfigurationManager.AppSettings["FolderPath"];
                savepath = Server.MapPath(tempPath);
                string filename = postedFile.FileName;
                string sExtension = filename.Substring(filename.LastIndexOf('.'));
                if (!Directory.Exists(savepath))
                    Directory.CreateDirectory(savepath);
                
                string sNewFileName = DateTime.Now.ToString("yyyyMMddhhmmsfff");
                postedFile.SaveAs(savepath + @"\" + sNewFileName + sExtension);
                Response.Write(tempPath + sNewFileName + sExtension);
                Response.StatusCode = 200;
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
            }
            return null;
        }
    }
}
